<?php
namespace app\blog\controller;
use cmf\controller\AdminBaseController;
use think\Db;
//定时任务
class SinglesummaryController extends AdminBaseController
{
    public function total(){
        //店铺营业额汇总
        //今天0点时间
        $today_time=strtotime('today');        
        $check=Db::name('TotalStatis')->where(array('ctime'=>array('egt',$today_time)))->find();/* 判断今天有没有汇总 */
        if($check){
            exit;
        }
         //昨天0点时间
        $yesterday_time=strtotime('yesterday');
        //判断不能重复汇总
        $list_store_list=Db::name('Store')->where(array('status'=>0))->select()->toarray();//正在运营的店铺列表
        $map['ctime'][]=array('gt',$yesterday_time);
        $map['ctime'][]=array('lt',$today_time);
        $map['paystatus']=1;//已付款
        foreach ($list_store_list as $k=>$v){
            $map['storeid']=$v['id'];
            $list=Db::name('OrderDetail')->where($map)->select()->toarray(); 
            echo Db::name('OrderDetail')->getlastsql();
            if($list){
                $total_money='';
                foreach ($list as $key=>$val){
                    $total_money+=$val['money'];
                }
                //把记录存数据库
                $data=array(
                    'storeid'=>$v['id'],
                    'money'=>$total_money,
                    'ctime'=>time()
                );
                $re=Db::name('TotalStatis')->insert($data);
            }
        }
    }
        
    public function single(){
        //单品/套餐营业额汇总
        //今天0点时间
        $today_time=strtotime('today');
        //昨天0点时间
        $yesterday_time=strtotime('yesterday');
        $check=Db::name('SingleStatis')->where(array('ctime'=>array('egt',$today_time)))->find();/* 判断今天有没有汇总 */
        if($check){
            exit;
        }
        $list_store_list=Db::name('Store')->where(array('status'=>0))->select()->toarray();//正在运营的店铺列表        
        foreach ($list_store_list as $k=>$v){
            //通过店铺列表获取每个店铺的商品id
            $goods_ids=$v['cat_ware'];
            $gid_arr=explode(',',chop($goods_ids,','));  //去掉字符串最后一个逗号  ，并把字符串换成数组
            foreach ($gid_arr as $kg=>$vg){
                $map['ctime']=array('gt',$yesterday_time);
                $map['ctime']=array('lt',$today_time);
                $map['paystatus']=1;//已付款
                $map['storeid']=$v['id'];
                $map['goodsid']=$vg;
                $map['stemeal']=0; 
                $total_money=Db::name('OrderDetail')->where($map)->sum('money');//该店铺该商品总营业额
                
                $num=Db::name('OrderDetail')->where($map)->count();////该店铺该商品总销售数量 
                if(!empty($total_money) || !empty($num)){
                    //把记录存数据库
                    $data=array(
                        'gid'=>$vg,
                        'storeid'=>$v['id'],
                        'money'=>$total_money,
                        'num'=>$num,
                        'type'=>1,
                        'ctime'=>time()
                    );
                    $re=Db::name('SingleStatis')->insert($data);
                }
            }
            //获取套餐列表id
            $setmeal_list=Db::name('Setmeal')->where(array('set_shopid'=>$v['id'],'set_end'=>array('gt',$yesterday_time)))->select()->toarray();             
            foreach ($setmeal_list as $ks=>$vs){
                $con['ctime']=array('gt',$yesterday_time);
                $con['ctime']=array('lt',$today_time);
                $con['paystatus']=1;//已付款
                $con['storeid']=$v['id'];
                $con['goodsid']=$vs['set_id'];
                $con['stemeal']=1;
                $total_moneys=Db::name('OrderDetail')->where($con)->sum('money');//数量
                $nums=Db::name('OrderDetail')->where($con)->count();
                //把记录存数据库
                $datas=array(
                    'storeid'=>$v['id'],
                    'money'=>$total_moneys,
                    'num'=>$nums,
                    'type'=>2,
                    'ctime'=>time(),
                    'gid'=>$vs['set_id']
                );
                $re=Db::name('SingleStatis')->insert($datas);
            }    
        }
    }
}
